<?php
/**
 * 后台默认首页
 *
 * @copyright Copyright © 2008 harry
 * @author harry 
 * @subpackage room
 * 
 */
FLEA::loadClass('admin_AdminAbstract');
class admin_AdminDefault extends admin_AdminAbstract
{
	/**
	 * 用户表
	 *
	 * @var unknown_type
	 */
	var $user;
	
	function __construct()
	{
		parent::__construct();
		$this->user = FLEA::getSingleton('table_SysUsers');
	}
	/**
	 * 后台首页
	 *
	 */
	function __AdminIndex()
	{
		echo url();
		dump($_SESSION);
		$this->smarty->display('admin/home.html');
	}
	/**
	 * 管理用户列表
	 *
	 */
	function __AdminUserList()
	{
		//echo FLEA::getAppInf('defaultControllerACTFile');
		if($_REQUEST['permissions']&&is_numeric($_REQUEST['permissions']))
		{
			$permissions =	$_REQUEST['permissions'];
		}else
		{
			$permissions = 2;
		}
		$userresult = $this->user->findAll("permissions=$permissions");
		//dump($userresult);
		$this->smarty->assign(array('user'=>$userresult));
		$this->smarty->display('admin/userlist.tpl');
	}
	/**
	 * 改变用户状态 （禁用，启用）
	 *
	 */
	function __AdminUserState()
	{
		$embargo = $_REQUEST['embargo'];
		if($embargo==1)
		{
			$embargo=0;
		}else
		{
			$embargo=1;
		}
		$conditions = array(
			'embargo' => $embargo,
			'user_id' => $_REQUEST['user_id'],
		);

		$this->user->save($conditions);

		redirect($_SERVER['HTTP_REFERER']);
	}
	/**
	 * 后台登陆
	 *
	 */
	function __AdminLogin()
	{
		$imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
		if($_POST){
			do {
				if (!$imgcode->check($_POST['imgcode'])) {
	                $msg = _T('ui_l_invalid_imgcode');
	                break;
	            }
	            $imgcode->clear();
				/**
	             * 验证用户名和密码是否正确
	             */
	            $sysusers =& FLEA::getSingleton('table_SysUsers');
	            /* @var $sysusers Model_SysUsers */
	            $user = $sysusers->findByUsername($_POST['username']);
	            if (!$user) {
	                $msg = _T('ui_l_invalid_username');
	                break;
	            }
	
	            if (!$sysusers->checkPassword($_POST['password'],
	                $user[$sysusers->passwordField]))
	            {
	                $msg = _T('ui_l_invalid_password');
	                break;
	            }
	
	            /**
	             * 登录成功，通过 RBAC 保存用户信息和角色
	             */
	            $data = array();
	            $data['USERNAME'] = $user[$sysusers->usernameField];
	            $data['ID'] = $user[$sysusers->primaryKey];
	
	            $rbac =& FLEA::getSingleton('FLEA_Rbac');
	            /* @var $rbac FLEA_Rbac */
	            $rbac->setUser($data, $sysusers->fetchRoles($user));
	
	            // 保存用户选择的语言
	            $_SESSION['LANG'] = $_POST['language'];
	            // 重定向
	            redirect(url('AdminDefault','AdminIndex'));
			}while(false);
			redirect(url('AdminDefault','AdminLogin'));
		}

		$this->smarty->display('admin/login.tpl');
	}
	/**
     * 显示验证码
     */
    function __AdminImgCode() {
        $imgcode =& FLEA::getSingleton('FLEA_Helper_ImgCode');
        /* @var $imgcode FLEA_Helper_ImgCode */
        $imgcode->image();
    }
	/**
	 * 插入一条用户
	 *
	 */
	function actionInsert(){
		  $sysusers =& FLEA::getSingleton('table_SysUsers');
		$row = array(
			'roles'=> array(array('rolename'=>'fsfdsfsdf')),//插入一条新的权限给新的用户
			'roles'=> array(3,1),//插入需要的权限id就可以获得该权限(如果只要一个就一个id多个就逗号给开)
			'username'=> 'yhrt',
			'password'=>'fdsf',
			'email' => 'fdsf@fds.s',
		);
		  $sysusers->save($row);
	}
}